package com.xinxin.leetcode.problem581;

import java.util.Arrays;

/**
 * @author ShiXinXin
 * @date 2020-02-07 14:36
 */
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int res = 0, start = 0, end = nums.length - 1;
        int[] num = new int[nums.length];
        System.arraycopy(nums, 0, num, 0, nums.length);
        Arrays.sort(nums);
        while (start <= end) {
            if (num[start] == nums[start]) {
                start++;
            } else {
                break;
            }
        }
        while (start <= end) {
            if (num[end] == nums[end]) {
                end--;
            } else {
                break;
            }
        }
        return end - start + 1;
    }

    public static void main(String[] args) {
        int[] arr = {2, 6, 4, 8, 10, 9, 15};
        System.out.println(new Solution().findUnsortedSubarray(arr));
    }
}
